package com.jxwifi.cloud.quickclean.cleanermap;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.utils.KAlertDialog;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.jxwifi.cloud.quickclean.utils.TokenTimeUtils;

import java.text.DecimalFormat;

public class CleaningMapPresenter {

    private Context mContext;
    private CleaningMapView mView;
    private AuzImpl mAuz;

    //初始化地图控制器对象
    AMap aMap;

    public CleaningMapPresenter(Context context, CleaningMapView view) {
        this.mContext = context;
        this.mView = view;
        mAuz = new AuzImpl();
    }

    public void initView(float distance,long duration) {

        //换算时间  服务的时间为：serviceTime 系统时间：now TokenTimeUtils.newTime(mAskBean.getServiceTime(),mAskBean.getNow())
        String mServiceTime = TokenTimeUtils.newTime(mView.getBean().getOrderInfo().getServiceTime(), mView.getBean().getOrderInfo().getNow());
        if (mServiceTime.indexOf(":") != -1) {
            mView.getMapWorkTimeHours().setText("小时");
            mView.getMapWorkTime().setText(mServiceTime);
        } else {
            mView.getMapWorkTime().setText(mServiceTime);
            mView.getMapWorkTimeHours().setText("天");
        }

        //double disprovn = DistanceOfTwoPoints(Double.valueOf(mView.getBean().getOrderInfo().getLat()), Double.valueOf(mView.getBean().getOrderInfo().getLon()), Double.valueOf(mView.getBean().getCleanerInfo().getLat()), Double.valueOf(mView.getBean().getCleanerInfo().getLon())) / 1000;
        if (distance > 1) {
            DecimalFormat format = new DecimalFormat("#0.00");
            String str = format.format(distance);
            mView.getMapWorkDistance().setText(str);
        } else {
            mView.getMapWorkDistance().setText("<1");
        }

    }

    //保洁员拨打电话
    public void CleaningTell() {
        /*Intent mTelintent = new Intent(Intent.ACTION_DIAL);
        Uri Urldata = Uri.parse("tel:" + mView.getBean().getCleanerInfo().getTell().replace("\\", ""));
        mTelintent.setData(Urldata);
        mContext.startActivity(mTelintent);*/
        Params params = new Params();
        params.add("orderCode", mView.getBean().getOrderInfo().getOrderCode());
        mAuz.auzget(UrlPool.GET_GENERATE_CATM, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code,final String data) {
                if (code == 0) {
                    if (!TextUtils.isEmpty(data)) {
                        final KAlertDialog dialogshow = new KAlertDialog(mContext, "确认当前手机号码是否和您的下单号码" + mView.getBean().getOrderInfo().getUserTell() + "一致");
                        dialogshow.setRightTxt("确定", new NoDoubleClickListener() {
                            @Override
                            protected void onNoDoubleClick(View v) {
                                Intent mTelintent = new Intent(Intent.ACTION_DIAL);
                                Uri Urldata = Uri.parse("tel:" + data.replace("\\", ""));
                                mTelintent.setData(Urldata);
                                mContext.startActivity(mTelintent);
                                dialogshow.dismiss();
                            }
                        });
                        dialogshow.show();
                    } else {
                        Toast toast = Toast.makeText(mContext, "保洁员电话与当前下单号相同，无法拨打", Toast.LENGTH_SHORT);
                        ToastUtils.showMyToast(toast, 3000);
                    }
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }


    private static final double EARTH_RADIUS = 6378137;

    public static double DistanceOfTwoPoints(double lat1, double lng1,
                                             double lat2, double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }
}
